package com.hhz.stream.part3;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;

/**
 * @Author Rem
 * @Date 2020-02-14
 */

public class TestForkJoin {


    /**
     * ForkJon
     */
    @Test
    public void test() {
        Instant start = Instant.now();

        ForkJoinPool pool = new ForkJoinPool();
        ForkJonCalculate forkJonCalculate = new ForkJonCalculate(0, 1000000L);
        Long sum = pool.invoke(forkJonCalculate);
        System.out.println(sum);

        Instant end = Instant.now();
        System.out.println("耗费时间:" + Duration.between(start, end).toMillis()); //8
    }

    /**
     * 普通for
     */
    @Test
    public void test2() {
        Instant start = Instant.now();
        long sum = 0L;
        for (Long i = 0L; i < 1000000L; i++) {
            sum += i;
        }
        System.out.println(sum);


        Instant end = Instant.now();
        System.out.println("耗费时间:" + Duration.between(start, end).toMillis());//22
    }
}
